#include <stdio.h>
#include <curses.h>
#include "sud_lev.h"
#include "sud_fonct.h"

#define ESC 27
#define DROITE 261
#define GAUCHE 260
#define HAUT 258
#define BAS 259
#define ENTER 10


void draw(void);
void deplacer(int *x, int *y, int key);
void ch_chiffre (int *x, int *y, int key);

int main(void)
{
   int key,i,j,x=1,y=1;
   bool ok=FALSE;

   /*initialisation de ncurses*/
   initscr();
   keypad(stdscr, TRUE);
   cbreak();
   noecho();
   draw(); /*afficher la grille*/

   for (i=0 ; i<9 ; i++) /* copie de tableaux pour tests*/
      for (j=0 ; j<9 ; j++)
         sudoku[i][j]=sud1[i][j];

   do {
      key=getch();
      if (key == ENTER && est_complet())
        if (test_sudoku_complet()) {move (1,25); printw("         sudoku bien rempli    ");}
        else {move(1,25); printw ("         sudoku mal rempli!     ");}
      if (key == ENTER && !est_complet())
        if (test_sudoku_incomplet()) {move (1,25); printw("         ok pour l'instant!        ");}
        else {move(1,25); printw ("        la grille est fausse    ");}

      if (key < 262 && key > 257) deplacer(&x,&y,key);
      if (key > 47 && key < 58) ch_chiffre(&x,&y,key);
   } while (key!=ESC);

   endwin();
   return 0;
}

void ch_chiffre (int *x, int *y, int key) {
   int k,l;

   if (sud1[*y-1][*x-1]==0) {
      sudoku[*y-1][*x-1] = key - 48; /*car getch retourne 48 pour la touche 0*/
      if ( *x < 4 ) k = *x;
      else if ( *x < 7 ) k = *x + 1;
      else k = *x + 2;
      if ( *y < 4 ) l = *y;
      else if ( *y < 7 ) l = *y + 1;
      else l = *y + 2;
      move (l,k);

      printw("%d",key - 48);
      move (l,k);
   }
}

void deplacer(int *x, int *y, int key) {
   int k,l;
   switch (key) {
      case HAUT:
         if (*y < 9) ++*y;
         break;
      case BAS:
         if (*y > 1) --*y;
         break;
      case GAUCHE:
         if (*x > 1) --*x;
         break;
      case DROITE:
         if (*x < 9) ++*x;
         break;
      }
   if ( *x < 4 ) k = *x;
   else if ( *x < 7 ) k = *x + 1;
   else k = *x + 2;
   if ( *y < 4 ) l = *y;
   else if ( *y < 7 ) l = *y + 1;
   else l = *y + 2;

   move (l,k);
}

/*Fonction super crade d'affichage de la grille*/
void draw(void) {
   int i,j,k;

   move (0,25); printw("     -=sudoku version 1.0beta=-");
   move (2,25); printw("    entrée pour valider la grille");
   move (3,25); printw("fleches de direction pour se deplacer");
   move (4,25); printw("        [echap] pour quitter");
   move (0,0);
   printw ("*-----------*");
   for (i = 0 ; i < 9 ; i++) {
      move (i+1,0); printw ("|"); move (i+1,12); printw("|");
      move (i+2,0); printw ("|"); move (i+2,12); printw("|");
      move (i+3,0); printw ("|"); move (i+3,12); printw("|");

      if (i < 3) k=i;
      else if ( i < 6 ) k=i+1;
      else k=i+2;
      if (i == 3) { move (k,1); printw("------------"); }
      if (i == 6) { move (k,1); printw("------------"); }

      for (j = 0 ; j < 9 ; j++) {
	 if (j == 3) { move (k+1,j+1); printw("|"); }
	 if (j == 6) { move (k+1,j+2); printw("|"); }
         if (j < 3) move (k+1,j+1);
	 else if ( j < 6 ) move (k+1,j+2);
         else move (k+1,j+3);
         if (sud1[i][j] == 0) printw (" ");
         else printw ("%d",sud1[i][j]);
      }
   }
   printw ("\n*-----------*");
   move(1,1);
}
